package game
import (
	"message"
	"github.com/name5566/leaf/log"
	"game/poker/hand"
	"github.com/golang/protobuf/proto"
)

// -----------------s-c 服务器通知客户端------------------------

// s-c 广播聊天信息
func (self *Room) broadcastChatMsg(str string, targetID int, senderID int) {
	sender, ok := self.players[senderID]
	if !ok {
		log.Error("broadcastChatMsg 消息发送者不存在: %v", senderID)
		return
	}

	if targetID > 0 {
		target, ok := self.players[targetID]
		if ok {
			// 发送给一个人
			target.SendMsg(message.MSG_CHAT, &message.S2C_Chat{
				Sender:proto.Int(sender.ID()), SenderName:proto.String(sender.data.Name),
				Target:proto.Int(targetID), TargetName:proto.String(target.data.Name),
				Message:proto.String(str),
			})
		}
	} else {
		// 广播
		for _, v := range self.players {
			if sender.ID() != senderID {
				v.SendMsg(message.MSG_CHAT, &message.S2C_Chat{
					Sender:proto.Int(sender.ID()), SenderName:proto.String(sender.data.Name),
					Message:proto.String(str),
				})
			}
		}
	}
}

// s-c 广播玩家坐下
func (self *Room) broadcastSitDown(player *User) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_SITDOWN, &message.S2C_PlayerInfo{
			PlayerInfo:player.getInfo(),
		})
	}
}

// s-c 玩家离玩家站起
func (self *Room) broadcastStandUp(seat int) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_STANDUP, &message.DataInt{
			Param:proto.Int(seat),
		})
	}
}

// s-c 广播玩家操作
func (self *Room) broadcastAction(player *User, action message.PokerAction, param int) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_ACTION, &message.S2C_Action{
			Seat:proto.Int(player.seat),
			Action:action.Enum(),
			Param:proto.Int(param),
		})
	}
}

// s-c 广播回合数据
func (self *Room) broadcastRound(cards...*hand.Card) {
	card1 := -1
	card2 := -1
	card3 := -1
	if len(cards) > 0 {
		card1 = cards[0].Index()
	}
	if len(cards) > 1 {
		card2 = cards[1].Index()
	}
	if len(cards) > 2 {
		card3 = cards[2].Index()
	}

	for _, v := range self.players {
		v.SendMsg(message.MSG_ROUND, &message.S2C_Round{
			Round:self.round.Enum(),
			Card1:proto.Int(card1),
			Card2:proto.Int(card2),
			Card3:proto.Int(card3),
		})
	}
}

// s-c 广播玩家属性改变
func (self *Room) broadcastPlayerAttrChanged(player *User, attr message.PlayerAttributeType, value int) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_PLAYER_ATTRIBUTE_CHANGED, &message.S2C_PlayerAttributeChanged{
			Attribute:[]*message.PlayerAttribute{&message.PlayerAttribute{Seat:proto.Int(player.seat), Type:attr.Enum(), Value:proto.Int(value)}},
		})
	}
}

// s-c 广播玩家亮牌
func (self *Room) broadcastShowCard(player *User) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_SHOW_CARD, &message.S2C_ShowCard{
			Seat:proto.Int(player.seat),
			Card1:proto.Int(player.handCard1.Index()),
			Card2:proto.Int(player.handCard2.Index()),
		})
	}
}

// s-c 广播房间属性改变（底池金额）
func (self *Room) broadcastRoomAttrChanged(attr message.RoomAttributeType, value int) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_ROOM_ATTRIBUTE_CHANGED, &message.S2C_RoomAttributeChanged{
			Attribute:[]*message.RoomAttribute{&message.RoomAttribute{Type:attr.Enum(), Value:proto.Int(value)}},
		})
	}
}

// s-c 广播比赛结果
func (self *Room) broadcastResult(result []*message.ResultInfo) {
	for _, v := range self.players {
		v.SendMsg(message.MSG_RESULT, &message.S2C_Result{
			Result:result,
		})
	}
}

// s-c 服务器逻辑发生错误，广播通知客户端
func (self *Room) broadcastError(err message.ErrorCode) {
	for _, v := range self.players {
		v.SendError(message.MSG_ERROR, err)
	}
}

// s-c 服务器广播通知底池变化
func (self *Room) broadcastPotChanged() {
	self.broadcastRoomAttrChanged(message.RoomAttributeType_ROOM_ATTR_POOL, self.getAllPots())
}

// s-c 服务器通知客户端自己的牌型是什么
func (self *Room) broadcastCardType() {
	for _, player := range self.players {
		_, ev, _ := self.evaluate(player)
		cardType := self.getCardTypeByScore(ev)
		player.SendMsg(message.MSG_CARD_TYPE, &message.S2C_CardType{
			CardType:cardType.Enum(),
		})
	}
}

